package com.smartrobot.smartrobot.activity;



import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.smartrobot.smartrobot.Config.Config;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pagerHolder.BasePagerHolder;
import com.smartrobot.smartrobot.pagerHolder.InformationPagerHolder1;
import com.smartrobot.smartrobot.pagerHolder.MessagePagerHolder1;
import com.smartrobot.smartrobot.pagerHolder.PersonalPagerHolder1;
import com.smartrobot.smartrobot.pagerHolder.SocialCirlePagerHolder;
import com.smartrobot.smartrobot.pagerHolder.SocialCirlePagerHolder1;

import java.util.ArrayList;
import java.util.List;

public class MainActivity_temp extends Activity{
	
	private ViewPager viewPager;
	private List<BasePagerHolder> holder;
    private RadioGroup bottomMenu;

	private InformationPagerHolder1 informationPagerHolder;
	private MessagePagerHolder1 messagePagerHolder;
	private PersonalPagerHolder1 personalPagerHolder;
	private SocialCirlePagerHolder1 socialCirclePagerHolder;
    private Context context;

	private boolean isStopFromSystem = false;
	static public MainActivity_temp instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.page_main);
		
		init();

        context = this.getApplicationContext();
        LogToSd.log_init();
        Config.config_init(context);
		instance = this;



//        FriendList friendList = new FriendList();
//        friendList.etFriendList();

        //读取 配置文件
        String text = Config.GetConfig().getProperty("httpaddr");

        //写入sd卡文件
        LogToSd.info(text);

    }


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		SocialCirlePagerHolder.changeFriendList( requestCode,  resultCode,  data);

	}

	void init(){
		
		viewPager = (ViewPager)findViewById(R.id.main_viewpager);
        viewPager.setOffscreenPageLimit(4);
        bottomMenu = (RadioGroup)findViewById(R.id.main_bottom_menu);


        bottomMenu.setOnCheckedChangeListener(new onBottomListener());
		informationPagerHolder = new InformationPagerHolder1(getApplicationContext(),LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_main_information, null));
		messagePagerHolder = new MessagePagerHolder1(getApplicationContext(),LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_main_message, null));
//		socialCirclePagerHolder = new SocialCirlePagerHolder1(getApplicationContext(),LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_main_social_circle, null));
		personalPagerHolder = new PersonalPagerHolder1(getApplicationContext(),LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_main_person_center, null));
		
		
		
		holder = new ArrayList<BasePagerHolder>();
		holder.add(informationPagerHolder);
		holder.add(messagePagerHolder);
		holder.add(socialCirclePagerHolder);
		holder.add(personalPagerHolder);




        // 重写ViewPager适配器
		PagerAdapter pagerAdapter = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View view, Object object) {
				// TODO Auto-generated method stub
				return view == object;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return holder.size();
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(holder.get(position).getView(), 0);
				return holder.get(position).getView();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {

                // 删除某个view
				((ViewPager) container).removeView(holder.get(position).getView());

			}

            @Override
            public void setPrimaryItem(ViewGroup container, int position, Object object) {



                super.setPrimaryItem(container, position, object);
            }
        };

        // 设置适配器
		viewPager.requestDisallowInterceptTouchEvent(true);
		viewPager.setAdapter(pagerAdapter);
		viewPager.setCurrentItem(0);
        informationPagerHolder.initView();
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {

                switch(i){
                    case 0:
                        informationPagerHolder.initView();
                        ((RadioButton)findViewById(R.id.main_information)).setChecked(true);
                        break;
                    case 1:
                        ((RadioButton)findViewById(R.id.main_message)).setChecked(true);
                        messagePagerHolder.initView();

                        break;
                    case 2:
                        ((RadioButton)findViewById(R.id.main_social_circle)).setChecked(true);
                        socialCirclePagerHolder.initView();

                        break;
                    case 3:
                        ((RadioButton)findViewById(R.id.main_personal_center)).setChecked(true);
                        personalPagerHolder.initView();

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }
	
	
	class onBottomListener implements RadioGroup.OnCheckedChangeListener {


        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int id) {

                switch(id){

                    case R.id.main_information:

                        viewPager.setCurrentItem(0);

                        break;
                    case R.id.main_message:

                        viewPager.setCurrentItem(1);
                        messagePagerHolder.initView();

                        break;
                    case R.id.main_social_circle:

                        viewPager.setCurrentItem(2);
                        socialCirclePagerHolder.initView();

                        break;
                    case R.id.main_personal_center:

                        viewPager.setCurrentItem(3);
                        personalPagerHolder.initView();

                        break;

                }
        }
    }

	@Override
	protected void onStop() {
		LogToSd.debug("-----------onStop-----------");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		LogToSd.debug("-----------onDestroy-----------");

        clear();

		super.onStop();
		super.onDestroy();

	}

    void clear(){

        holder.clear();
        holder = null;

    }

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
//		LogToSd.debug("-----------onRestoreInstanceState-----------");
		Log.e("-----------------onRestoreInstanceState---------------------","onRestoreInstanceState");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

//		LogToSd.debug("-----------onSaveInstanceState-----------");
		Log.e("-----------------onSaveInstanceState---------------------","onSaveInstanceState");
		isStopFromSystem = true;
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestart() {

//		if(isStopFromSystem)
//		{
//			HttpUtil.init();
//			isStopFromSystem = false;
//		}
//		LogToSd.debug("-----------onSaveInstanceState-----------");
		Log.e("-----------------onRestart---------------------","onRestart");
		super.onRestart();
	}


}
